﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using woaC2.LogicLib.DataContract.Input;
using woaPoker.LogicLib.Database;
using woaC2.LogicLib.GlobalShare;
using woaC2.SocketLib;
using woaPoker.LogicLib.LogicCtrl;
using woaC2.LogicLib.DataContract.Output;

namespace woaPoker.LogicLib.LogicCtrl
{
    class LoginGameLgCtrl
    {
        public bool DangNhap(Guid sessionId, DangNhapInDC dangnhap)
        {
            TankDataContext db = new TankDataContext();
            User user = db.Users.Where(k => k.Username == dangnhap.username && k.Password == dangnhap.password).SingleOrDefault();
            if (user!=null)
            {
                var player =new Player(ClientRegistry.instance.GetById(sessionId),user.UserID);
                PlayerRegistry.instance.AddPlayer(player);
                return true;
            }
            else return false;
         
        }
        public User GetInfoUser(Guid sessonId)
        {
            TankDataContext db = new TankDataContext();
            Client client = ClientRegistry.instance.GetById(sessonId);
            if (client == null)
            {
                return null;
            }
            else
            {
                Player player= PlayerRegistry.instance.GetPlayer(sessonId);
                return db.Users.Where(k => k.UserID == player.UserID).SingleOrDefault();
            }
        }
        public List<GetListMatchFreeOutDc> GetListRoom()
        {
            List<GetListMatchFreeOutDc> l = new List<GetListMatchFreeOutDc>();
            List<Match> list= MathRegistry.instance.GetAllMatchFree();
            foreach (Match a in list)
            {
                if (a.status == false)
                {
                    GetListMatchFreeOutDc b = new GetListMatchFreeOutDc();
                    b.sessionIdMatch = a.sessionIdMatch;
                    b.matchName = a.MatchName;
                    l.Add(b);
                }
            }
            return l;
        }
        public LoginGuestOutDc LoginGuest(Guid sessionId)
        {

            var player = new Player(ClientRegistry.instance.GetById(sessionId), 0);
            PlayerRegistry.instance.AddPlayer(player);
            LoginGuestOutDc objOut = new LoginGuestOutDc();
            objOut.Id = sessionId;
            objOut.username = player.userName;
            return objOut;
        }


    }
}
